package proiectpractica;

import java.sql.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Add_Material.java
 *
 * Created on Jun 30, 2010, 5:44:56 PM
 */

/**
 *
 * @author Claudiu
 */
public class Add_Material extends javax.swing.JFrame {

    /** Creates new form Add_Material */
    Connection con;
    Statement st;
    int val;
    ArrayList<String > furnizoriNume;
    ArrayList<Integer> furnizoriId;

    public Add_Material() {
        initComponents();

    }
        public Add_Material(Connection c) {
        initComponents();
        con=c;
        furnizoriNume = new ArrayList<String>();
        furnizoriId = new ArrayList<Integer>();
        JOptionPane.showMessageDialog(null, "Form pentru Introducerea unui nou material in baza de date \n Nu lasti nici un camp necompletat!");
        try {
            st = con.createStatement();


            ResultSet rs=st.executeQuery("select idfurnizor , nume  from furnizor");
           
            while (rs.next())
            {
                furnizoriId.add(rs.getInt(1));
                furnizoriNume.add(rs.getString(2));
                furnizorBox.addItem(rs.getString(2));

            }
//             rs= st.executeQuery("select cod_client from scott.client");
//              while (rs.next())
//            {
//               clientBox.addItem(rs.getInt(1));
//
//            }
            // }
        } catch (Exception ex) {
            Logger.getLogger(Add_Furnizor.class.getName()).log(Level.SEVERE, null, ex);
                             JOptionPane.showMessageDialog(null, ex.getMessage());
        }


    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        furnizorBox = new javax.swing.JComboBox();
        tipField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        ExitjButton1 = new javax.swing.JButton();
        addjButton2 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        NumeField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        descriereField = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        pretField1 = new javax.swing.JTextField();

        jLabel1.setText("jLabel1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Adaugare Material");

        furnizorBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                furnizorBoxActionPerformed(evt);
            }
        });

        jLabel2.setText("Nume Furnizor");

        jLabel4.setText("tip");

        ExitjButton1.setText("exit");
        ExitjButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ExitjButton1ActionPerformed(evt);
            }
        });

        addjButton2.setText("add");
        addjButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addjButton2ActionPerformed(evt);
            }
        });

        jLabel5.setText("nume");

        jLabel6.setText("descriere");

        jLabel7.setText("pret");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(furnizorBox, 0, 154, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel6))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(descriereField)
                                    .addComponent(tipField)
                                    .addComponent(NumeField, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(addjButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ExitjButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jLabel7)
                        .addGap(42, 42, 42)
                        .addComponent(pretField1, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(furnizorBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(NumeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(tipField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(descriereField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(pretField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addjButton2)
                    .addComponent(ExitjButton1))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void furnizorBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_furnizorBoxActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_furnizorBoxActionPerformed

    private void addjButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addjButton2ActionPerformed
        // TODO add your handling code here:
         //val++;
        try {
        if(NumeField.getText().trim().equalsIgnoreCase("") || tipField.getText().trim().equalsIgnoreCase("") || descriereField.getText().trim().equalsIgnoreCase("") || pretField1.getText().trim().equalsIgnoreCase(""))
        {
            throw new Exception("Date Intrare Increcte !");
        }
            
            CallableStatement cs = con.prepareCall("{call addMaterialp(?,?,?,?,?)}");
            cs.setInt(1, furnizoriId.get( furnizorBox.getSelectedIndex()));
            cs.setString(2, NumeField.getText());
            cs.setString(3, tipField.getText());
            cs.setString(4, descriereField.getText());
            cs.setInt(5, Integer.parseInt(pretField1.getText()));
            int aux= cs.executeUpdate();

          //st.executeUpdate("call addFurnizorproc ( '"+nume_box.getText()+" ' ,'"+address_box.getText()+" ') ");
            //  st.executeUpdate("insert into scott.client (cod_client , nume , cnp , address) values( '" + val + "' , '" + nume_box.getText() + "' , '" + cnp_box.getText() + "' , '" + address_box.getText()+"' )");
           con.commit();

           JOptionPane.showMessageDialog(null, "Cererea Dvs. a fost transmisa catre Baza  de Date pentru procesare");
        } catch (Exception ex) {
            Logger.getLogger(Add_Furnizor.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }//GEN-LAST:event_addjButton2ActionPerformed

    private void ExitjButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ExitjButton1ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_ExitjButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Add_Material().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ExitjButton1;
    private javax.swing.JTextField NumeField;
    private javax.swing.JButton addjButton2;
    private javax.swing.JTextField descriereField;
    private javax.swing.JComboBox furnizorBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField pretField1;
    private javax.swing.JTextField tipField;
    // End of variables declaration//GEN-END:variables

}
